<?php

class Admin_CategoryController extends Admin_Library_Controller_Action
{

    public function init()
    {
        parent::init();
        $this->_model = new Model_Category();
        $this->_form = new Admin_Form_Category();

        if (in_array($this->getRequest()->getActionName(), array('index', 'list'))) {
            $config = Zend_Registry::get('configuration');
            $page = 1;
            if (isset($this->_params['page']) && $this->_params['page'] > 0) {
                $page = $this->_params['page'];
            }
            $limit = $config->site->backend->paging->items_per_page;
            if (isset($this->_params['itemperpage']) && isset($this->_params['itemperpage']) > 0) {
                $limit = $this->_params['itemperpage'];
            }
            $orders = array('category.created_date DESC', 'category.name');
            $wheres = array();
            $orWheres = array();
            $likes = array();
            $orLikes = array();



            if (isset($this->_params['keyword']) && $this->_params['keyword'] !== '') {
                $likes['category.name'] = '%' . strip_tags($this->_params['keyword']) . '%';
                $this->view->assign('keyword', strip_tags($this->_params['keyword']));
            }
            if (isset($this->_params['published']) && $this->_params['published'] != '') {
                $wheres['category.published'] = intval($this->_params['published']);
                $this->view->assign('published', intval($this->_params['published']));
            }

            $this->_page = $page;
            $this->_limit = $limit;
            $this->_orders = $orders;
            $this->_wheres = $wheres;
            $this->_orWheres = $orWheres;
            $this->_likes = $likes;
            $this->_orLikes = $orLikes;
        }
    }

    public function indexAction()
    {
        $this->_forward('list');
    }

    public function editAction()
    {
        if ($this->getRequest()->isPost() && $this->_form->isValid($this->getRequest()->getPost())) {
            $result = $this->save();
            $this->_helper->flashMessenger->addMessage($result);
            if ($result['result'] == 'success') {
                $this->_helper->redirector->gotoRoute($this->_defaultUrlOptions);
            }
        }

        $item = $this->_model->find(intval($this->getRequest()->getParam('id')));
        $values = $item->toArray();
        if ($values['published'] == 1) {
            $values['published'] = 'yes';
        } else if ($values['published'] == 0) {
            $values['published'] = 'no';
        }

        $this->_form->populate($values);

        $this->view->form = $this->_form;
        $this->_helper->viewRenderer('add');
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost() && $this->_form->isValid($this->getRequest()->getPost())) {
            $result = $this->save();
            $this->_helper->flashMessenger->addMessage($result);
            if ($result['result'] == 'success') {
                $this->_helper->redirector->gotoRoute($this->_defaultUrlOptions);
            }
        }

        $this->_form->getElement('section_id')->setValue(intval($this->view->config->section->news_id));
        $this->view->form = $this->_form;
    }

}